package com.solution.admin.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.order.entity.OrderProductDeliveredEntity;
import com.solution.admin.order.service.OrderProductDeliveredService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 已发货商品web控制层
 * @author lilp
 * @date 2017-09-18 22:58:13
 */
@RequestMapping("/orderProductDelivered")
@Controller
public class OrderProductDeliveredController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(OrderProductDeliveredController.class);

	@Autowired
	private OrderProductDeliveredService orderProductDeliveredService;
	
	/**
	 * 跳转已发货商品查询页面
	 * @param OrderProductDelivered 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-18 22:58:13
	 */
	@RequestMapping("/toListOrderProductDeliveredPage")
	public String toListOrderProductDeliveredPage(OrderProductDeliveredEntity orderProductDelivered, HttpServletRequest request) {
		return "order/orderProductDelivered_list";
	}

	/**
	 * 分页查询已发货商品列表
	 * @param orderProductDelivered 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-18 22:58:13
	 */
	@RequestMapping("/listOrderProductDeliveredPage")
	@ResponseBody
	public JSONGrid listOrderProductDeliveredPage(OrderProductDeliveredEntity orderProductDelivered, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = orderProductDeliveredService.listOrderProductDeliveredPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询已发货商品失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 22:58:13
	 */
	@RequestMapping("/toAddOrderProductDelivered")
	public String toAddOrderProductDelivered(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "order/orderProductDelivered_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 22:58:13
	 */
	@RequestMapping("/toUpdateOrderProductDelivered")
	public String toUpdateOrderProductDelivered(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			OrderProductDeliveredEntity orderProductDelivered = orderProductDeliveredService.getOrderProductDeliveredById(id);
			request.setAttribute("orderProductDelivered", orderProductDelivered);
		} catch (Exception e) {
			handleException("跳转编辑已发货商品页面错误", logger, e);
		}
		return "order/orderProductDelivered_form";
	}


	/**
	 * 新增或保存已发货商品
	 * @param request 请求对象
	 * @param orderProductDeliveredEntity 已发货商品实体
	 * @return
	 * @author lilp
 	 * @date 2017-09-18 22:58:13
	 */
	@RequestMapping("/saveOrUpdateOrderProductDelivered")
	@ResponseBody
	public Map<String, Object> saveOrderProductDelivered(HttpServletRequest request, OrderProductDeliveredEntity orderProductDeliveredEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isEmpty(id)){
				result = orderProductDeliveredService.updateOrderProductDelivered(orderProductDeliveredEntity);
			}else{
				result = (int)orderProductDeliveredService.saveOrderProductDelivered(orderProductDeliveredEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加已发货商品失败", logger, e);
		}
		return resultMap;
	}
	
}
